<template>
  <el-card shadow="hover" class="mt-20">
    <!-- 
      category3Id 有值 不禁用
      category3Id 没值 禁用
     -->
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddSpu"
    >
      添加SPU
    </el-button>

    <el-table :data="spuList" border class="spu-list" v-loading="loading">
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作" width="220">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            :icon="Plus"
            size="small"
            title="添加SKU"
            @click="showAddSku(row)"
          ></el-button>
          <el-button
            type="warning"
            :icon="Edit"
            size="small"
            title="修改SPU"
            @click="showUpdateSpu(row)"
          ></el-button>
          <el-button
            type="info"
            :icon="InfoFilled"
            size="small"
            title="查看SKU列表"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            size="small"
            title="删除SPU"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getSpuList"
      @current-change="getSpuList"
    />
  </el-card>
</template>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Plus, Edit, InfoFilled, Delete } from "@element-plus/icons-vue";
import { getSpuListApi } from "@/api/product/spu";
import type { SpuList, SpuItem } from "@/api/product/models/spuModels";
import { useCategoryStore } from "@/stores/category";
import { useSpuStore } from "@/stores/spu";

defineOptions({
  name: "SpuList",
});

// 3. 显示 添加 SKU
const showAddSku = (row: SpuItem) => {
  emit("setIsShow", 3);
  spuStore.$patch({
    id: row.id,
    spuName: row.spuName,
    // 其他数据不需要，就不更新了
  });
};

// 2. 显示 添加 或 更新 SPU
const emit = defineEmits(["setIsShow"]);

const showAddSpu = () => {
  emit("setIsShow", 2);
  spuStore.$patch({
    id: undefined,
    spuName: "",
    tmId: undefined,
    description: "",
  });
};

const spuStore = useSpuStore();

const showUpdateSpu = (row: SpuItem) => {
  emit("setIsShow", 2);
  spuStore.$patch({
    id: row.id,
    spuName: row.spuName,
    tmId: row.tmId,
    description: row.description,
  });
};

// 1. SpuList 数据分页展示
const spuList = ref<SpuList>([]);
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const loading = ref(false);

const categoryStore = useCategoryStore();

const getSpuList = async () => {
  loading.value = true;
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  total.value = res.total;
  spuList.value = res.records;
  loading.value = false;
};

watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      total.value = 0;
      currentPage.value = 1;
      pageSize.value = 3;
      return;
    }
    getSpuList();
  },
  {
    immediate: true,
  }
);
</script>

<style scoped lang="scss">
.spu-list {
  margin: 20px 0;
}
</style>
